package com.thread;

import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author: mayu
 * @date: 2021年04月14日 18:07
 */
public class TestLinkedBlockingQueue {
    static BlockingQueue<String> bq = new LinkedBlockingQueue<>(10);

    static Random r = new Random();

    public static void main(String[] args) {
        //生产者
        new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                try {
                    bq.put("a" + i);
                    TimeUnit.MILLISECONDS.sleep(r.nextInt(1000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        //消费者
        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                for (; ; ) {
                    try {
                        System.out.println(Thread.currentThread().getName() + "take--" + bq.take());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }, "c" + i).start();
        }
    }
}
